package com.bw.module_cashier.view

import android.annotation.SuppressLint
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.bw.lib_common.entity.CashierPayerEntity
import com.bw.module_cashier.R
import com.bw.module_cashier.adapter.BillDetailAdapter
import com.bw.module_cashier.databinding.ActivityBillDetailBinding
import com.bw.module_cashier.viewmodel.CashierViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 智慧园区-05-收银台V1.1-20250512
 */
@Route(path = RouterPath.PATH_BILL_ACTIVITY)
@AndroidEntryPoint
class BillDetailActivity : BaseActivity<ActivityBillDetailBinding>() {
    private val vm by lazy { ViewModelProvider(this)[CashierViewModel::class.java] }
    @Autowired(name = "cashierPayerEntity")
    lateinit var entity: CashierPayerEntity
    var i: Int = 1
    private val billDetailAdapter by lazy { BillDetailAdapter() }
    private var total = 0
    private var hj = 0

    @SuppressLint("SetTextI18n")
    override fun initView() {
        ARouter.getInstance().inject(this)
        mDataBinding.projectName.text = entity?.projectName
        mDataBinding.payerName.text = "户主：" + entity?.payerName
        mDataBinding.contactNumber.text = "联系电话：" + entity?.contactNumber
        mDataBinding.totalAmount.text = "￥" + entity?.totalAmount + ".00"
        mDataBinding.rv.adapter = billDetailAdapter
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)
        mDataBinding.checkAll.setOnClickListener {
            billDetailAdapter.data.forEach {
                it.checked = mDataBinding.checkAll.isChecked
                total = if (it.checked) total + it.receivableAmount else total - it.receivableAmount
            }
            billDetailAdapter.notifyDataSetChanged()
            mDataBinding.total.text = "￥${total}.00"
        }
        billDetailAdapter.addChildClickViewIds(R.id.check)
        billDetailAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.check -> {
                    billDetailAdapter.data[position].checked =
                        !billDetailAdapter.data[position].checked
                    billDetailAdapter.notifyDataSetChanged()
                    mDataBinding.checkAll.isChecked =
                        billDetailAdapter.data.filter { it.checked }.size == billDetailAdapter.data.size
                    total =
                        if (billDetailAdapter.data[position].checked) total + billDetailAdapter.data[position].receivableAmount else total - billDetailAdapter.data[position].receivableAmount
                    mDataBinding.total.text = "￥${total}.00"
                }
            }
        }
        billDetailAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.PATH_CHARGE_ACTIVITY)
                .withSerializable("cashierChargeEntity", billDetailAdapter.data[position])
                .withSerializable("cashierPayerEntity", entity)
                .navigation()
        }
        mDataBinding.sf.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_TEMPORARY_CHARGES_ACTIVITY)
                .withSerializable("cashierPayerEntity", entity).navigation()
        }
        mDataBinding.pay.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_BILL_PAY_ACTIVITY)
                .withSerializable("total", total).withSerializable("cashierPayerEntity", entity)
                .navigation()
        }

        vm.cashierDetailBillSuccess.observe(this) {
            billDetailAdapter.data.clear()
            billDetailAdapter.data.addAll(it)
            billDetailAdapter.notifyDataSetChanged()
            hj = 0
            billDetailAdapter.data.forEach {
                hj += it.receivableAmount
            }
            mDataBinding.hj.text = "合计：￥${hj}.00"
        }
        mDataBinding.imageView.setOnClickListener {
            finish()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_bill_detail

    override fun onResume() {
        super.onResume()
        vm.getCashierDetailBill(i)
    }


}